const { Controller } = require('egg');

class PriceController extends Controller {


  /**
   * @api {GET} /product/price/list 获取价格列表
   * @apiName list
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } product_id 产品id
   * @apiQuery  { String } remark 备注
   * @apiQuery  { String } type 类型
   * @apiQuery  { String } is_expires 是否到期
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 排序方式
   * @apiQuery  { Number } pageIndex 页码
   * @apiQuery  { Number } pageSize 页面容量
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async list() {
    const ctx = this.ctx;
    const app = this.app;

    try {

      const value = ctx.query;

      const { product_id, remark, type, is_expires, order, method, pageIndex, pageSize } = await app.rule.price.list(value);

      const data = await ctx.service.price.list(product_id, remark, type, is_expires, order, method, pageIndex, pageSize);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /product/price/insert 新增
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } product_id 产品id
   * @apiBody  { String } price 价格
   * @apiBody  { String } remark 描述
   * @apiBody  { String } type 类型
   * @apiBody  { String } end_time 到期时间
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async insert() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { product_id, price, remark, type, end_time } = await app.rule.price.insert(value);

      const data = await ctx.service.price.insert(product_id, price, remark, type, end_time);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /product/price/update 更新价格
   * @apiName update
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json

   * @apiBody  { String } remark 描述
   * @apiBody  { String } id 价格id
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async update() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { remark, id } = await app.rule.price.update(value);

      const data = await ctx.service.price.update(remark, id);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {POST} /product/price/delete 更新价格
   * @apiName delete
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json

   * @apiBody  { Array } ids 价格ids
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async delete() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { ids } = await app.rule.price.delete(value);

      const data = await ctx.service.price.delete(ids);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = PriceController;
